import { Vector2D } from '../utils/Vector2D';
import { MainMenu } from './menus/MainMenu';
import { GameModeMenu } from './menus/GameModeMenu';
import { SettingsMenu } from './menus/SettingsMenu';
import { SkinShopMenu } from './menus/SkinShopMenu';
import { SkinSystem } from './SkinSystem';
import { BaseMenu } from './menus/BaseMenu';
import { UpgradeMenu } from './menus/UpgradeMenu';
import { ProgressionManager } from './ProgressionManager';

export type GameModeType = 'classic' | 'timeAttack' | 'maze';
export type SpeedLevel = 'slow' | 'normal' | 'fast';

export interface MenuConfig {
    speedLevel: SpeedLevel;
    customSpeed: number;
    currentMode: GameModeType;
    unlockedModes: GameModeType[];
    soundVolume: number;
    controlType: 'keyboard' | 'touch';
    graphicsQuality: 'low' | 'medium' | 'high';
}

export class MenuManager {
    private config: MenuConfig;
    private currentMenu: string = 'main';
    private menuStack: string[] = [];
    private mainMenu: MainMenu;
    private gameModeMenu: GameModeMenu;
    private settingsMenu: SettingsMenu;
    private skinShopMenu: SkinShopMenu;
    private upgradeMenu: UpgradeMenu;
    private skinSystem: SkinSystem;
    private progressionManager: ProgressionManager;

    constructor(skinSystem: SkinSystem, progressionManager: ProgressionManager) {
        this.config = this.loadConfig();
        this.skinSystem = skinSystem;
        this.progressionManager = progressionManager;
        this.initializeMenus();
    }

    private loadConfig(): MenuConfig {
        const savedConfig = localStorage.getItem('gameConfig');
        if (savedConfig) {
            try {
                return JSON.parse(savedConfig);
            } catch (e) {
                console.error('配置加载失败:', e);
            }
        }
        
        // 默认配置
        return {
            speedLevel: 'normal',
            customSpeed: 5.0,
            currentMode: 'classic',
            unlockedModes: ['classic'],
            soundVolume: 0.5,
            controlType: 'keyboard',
            graphicsQuality: 'medium'
        };
    }

    private initializeMenus(): void {
        this.mainMenu = new MainMenu(this.config);
        this.gameModeMenu = new GameModeMenu(this.config);
        this.settingsMenu = new SettingsMenu(this.config);
        this.skinShopMenu = new SkinShopMenu(this.config, this.skinSystem);
        this.upgradeMenu = new UpgradeMenu(this.config, this.progressionManager);
    }

    private saveConfig(): void {
        try {
            localStorage.setItem('gameConfig', JSON.stringify(this.config));
        } catch (e) {
            console.error('配置保存失败:', e);
        }
    }

    public navigateTo(menu: string): void {
        this.menuStack.push(this.currentMenu);
        this.currentMenu = menu;
    }

    public navigateBack(): boolean {
        if (this.menuStack.length > 0) {
            this.currentMenu = this.menuStack.pop()!;
            return true;
        }
        return false;
    }

    public getCurrentMenu(): string {
        return this.currentMenu;
    }

    public getCurrentMenuInstance(): BaseMenu | null {
        switch (this.currentMenu) {
            case 'main':
                return this.mainMenu;
            case 'modes':
                return this.gameModeMenu;
            case 'settings':
                return this.settingsMenu;
            case 'shop':
                return this.skinShopMenu;
            case 'upgrade':
                return this.upgradeMenu;
            default:
                return null;
        }
    }

    public isModeUnlocked(mode: GameModeType): boolean {
        return this.config.unlockedModes.includes(mode);
    }
    
    public getSkinSystem(): SkinSystem {
        return this.skinSystem;
    }

    public getConfig(): MenuConfig {
        return { ...this.config };
    }
}